$(document).on('ready', function(e)
{
  /*
   * Aceptar un comentario
   */
  $('a[data-approve]').on('click', function(e)
  {
    e.preventDefault();
    var $BtnApprove = $(this);
    var comment_id = $BtnApprove.data('approve');
    $.ajax('comentarios/actions.php?action=approve',{
      data: {'comment_id': comment_id},
      dataType: 'json',
      type: 'post',
      error: function(jqXHR, status_text, error_thrown)
      {
        alert('Ocurrió un error al procesar la solicitud (' + error_thrown + ')');
      },
      success: function(Response, status_text, jqXHR)
      {
        if(Response.error){
          alert(Response.message);
        } else {
          // Eliminar el boton de aprobación, el comentario ya esta aprobado
          $BtnApprove.remove();
        }
      }
    });
  });

  /*
   * Borrar un comentario
   */
  $('a[data-delete]').on('click', function(e)
  {
    e.preventDefault();
    var comment_id = $(this).data('delete');
    var $RowComment = $('#row_comment_' + comment_id);

    $.ajax('comentarios/actions.php?action=delete',{
      data: {'comment_id': comment_id},
      dataType: 'json',
      type: 'post',
      beforeSend: function(jqXHR, Settings)
      {
        $RowComment.addClass('deleting');
      },
      complete: function(jqXHR, status_text)
      {
        if($RowComment){
          $RowComment.removeClass('deleting');
        }
      },
      error: function(jqXHR, status_text, error_thrown)
      {
      },
      success: function(Response, status_text, jqXHR)
      {
        if(Response.error){
          alert(Response.message);
        } else {
          $RowComment.fadeOut('slow', function()
          {
            $(this).remove();
          });
        }
      }
    });
  });
});